import axios from 'axios';

export const getHomeInfoAction = () => {
    return dispatch => {
        axios
            .get('/api/home.json')
            .then(res => {
                const { topicList, list, writerList } = res.data.data;
                const data = { topicList, list, writerList };
                const action = {
                    type: 'HOME_INFO',
                    data,
                };
                dispatch(action);
            })
            .catch(error => {
                console.log(error);
            });
    };
};

export const getMoreListAction = page => {
    return dispatch => {
        axios
            .get('/api/homeList.json?page=' + page)
            .then(res => {
                const { list } = res.data.data;
                const action = {
                    type: 'HOME_LIST',
                    list,
                    page: page + 1,
                };
                dispatch(action);
            })
            .catch(error => {
                console.log(error);
            });
    };
};

export const changeScrollAction = () => {
    return dispatch => {
        let showScroll = document.documentElement.scrollTop > 300;
        const action = {
            type: 'CHANGE_SCROLL',
            showScroll,
        };
        dispatch(action);
    };
};
